package com.csg.ses.controller;

import com.csg.ses.common.annotation.LoginToken;
import com.csg.ses.entity.ses.MenuItemEntity;
import com.csg.ses.service.ses.MenuItemService;
import com.csg.ses.vo.common.IdListVo;
import com.csg.ses.vo.common.Result;
import com.csg.ses.vo.menu.ItemVo;
import com.csg.ses.vo.menu.MenuItemVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 菜单
 * @author wangjun
 * @date 2020/11/16
 */
@RestController
@RequestMapping("/menu")
@Api(tags = "菜单接口")
public class MenuItemController {
    @Autowired
    private MenuItemService menuItemService;

    /**
     * 获取菜单信息
     */
    @ApiOperation(value = "获取所有菜单项")
    @GetMapping("/list")
    @LoginToken
    public Result<List<MenuItemVo>> list() {
        return menuItemService.retrieve();
    }


    /**
     * 获取菜单信息
     */
    @ApiOperation(value = "获取所有菜单项")
    @GetMapping("/list-all")
    @LoginToken
    public Result<List<MenuItemVo>> listAll() {
        return menuItemService.retrieveAll();
    }

    @ApiOperation(value = "菜单列表分页查询")
    @ApiImplicitParams({
        @ApiImplicitParam(name = "item", value = "菜单信息", dataType = "MenuItemEntity", paramType = "body"),
        @ApiImplicitParam(name = "current", value = "当前页", required = true ,dataType = "Long"),
        @ApiImplicitParam(name = "size", value = "每页记录数", required = true ,dataType = "Long")
    })
    @GetMapping("/page-data")
    @LoginToken
    public Result<List<MenuItemEntity>> pageData(MenuItemEntity item, Long current, Long size) {
        return menuItemService.pageData(item, current, size);
    }

    /**
     * 菜单下拉树
     * @return
     */
    @ApiOperation(value = "菜单下拉树")
    @GetMapping("/tree")
    @LoginToken
    public Result<List<MenuItemVo>> tree() {
        return menuItemService.tree();
    }

    /**
     * 根据上级菜单编码获取菜单项
     * @param parentCode
     * @return
     */
    @ApiOperation(value = "根据上级菜单编码获取菜单项")
    @ApiImplicitParam(name = "parentCode", value = "上级菜单编码", dataType = "String", required = true)
    @GetMapping("/list/{parentCode}")
    @LoginToken
    public Result<List<MenuItemVo>> getByParentCode(@PathVariable String parentCode) {
        return menuItemService.getByParentCode(parentCode);
    }

    /**
     * 删除菜单项
     * @param idList
     * @return
     */
    @ApiOperation(value = "删除菜单项")
    @ApiImplicitParam(name = "idList", value = "菜单项ID数组", dataType = "IdListVo", paramType = "body", required = true)
    @DeleteMapping("/del")
    @LoginToken
    public Result<String> del(@RequestBody IdListVo idList) {
        return menuItemService.del(idList);
    }

    /**
     * 保存菜单项
     * @param itemVo
     * @return
     */
    @ApiOperation(value = "保存菜单项", notes = "1.上级编码parentCode不能为空！2.新增时，不需要传id，上级菜单编码parentCode不能为空；菜单编码menuCode和排序号orderCode如果不填，则自动生成；" +
            "3.修改时，id可以不传；菜单项menuCode不能为空，否则会新增一个菜单项；")
    @ApiImplicitParam(name = "itemVo", value = "菜单项数组", dataType = "ItemVo", paramType = "body", required = true)
    @PostMapping("/save")
    @LoginToken
    public Result<List<MenuItemVo>> save(@RequestBody ItemVo itemVo) {
        return menuItemService.save(itemVo);
    }

    @ApiOperation(value = "根据id查找菜单项")
    @ApiImplicitParam(name = "id", value = "菜单id", dataType = "String", required = true)
    @GetMapping("/{id}")
    @LoginToken
    public Result<MenuItemEntity> getById(@PathVariable String id) {
        return menuItemService.getById(id);
    }
}